package com.sxpcwlkj.framework.config;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil;
import com.sxpcwlkj.common.enums.DeviceEnum;
import com.sxpcwlkj.common.enums.ErrorCodeEnum;
import com.sxpcwlkj.common.exception.LoginException;
import com.sxpcwlkj.common.utils.MapstructUtil;
import com.sxpcwlkj.common.utils.StringUtil;
import com.sxpcwlkj.framework.constant.RedisConstant;

import com.sxpcwlkj.common.utils.RedisUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @Description TODO
 * @Author sxpcwlkj
 * @Version v1.0.0
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class LoginObject<T> {

    Class<T> clazz;

    public void TestNew(Class<T> clazz) {
        this.clazz = clazz;
    }

    public Class<T> getTClass() {
        return clazz;
    }


    /**
     * 是否登录
     *
     * @return
     */
    public static Boolean isLogin() {
        return StpUtil.isLogin();
    }

    /**
     * 获取当前登录用户ID
     */
    public static String getLoginId() {
        SaSession session = StpUtil.getSession();
        return session != null ? session.getLoginId().toString() : null;
    }

    /**
     * 获取当前登录用户ID
     */
    public static Boolean getLoginSuper() {
        if (Objects.equals(getLoginId(), "1")) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    /**
     * 保存当前登录用户
     */
    public static Boolean setLoginObject(Object clazz) {
        if (isLogin()) {
            try {
                if (getLoginId() != null) {
                    RedisUtil.setCacheObject(RedisConstant.USER_KEY + StpUtil.getLoginIdAsLong(), clazz);
                    return Boolean.TRUE;
                }
            } catch (Exception ignored) {
                log.info("保存登录对象:'{}',失败!", clazz);
            }
        }
        //请先登录
        log.error("请先登录后再进行保存！");
        return Boolean.FALSE;
    }

    /**
     * 获取当前登录用户
     */
    public static <T> T getLoginObject(Class<T> clazz) {
        if (isLogin()) {
            try {
                if (getLoginId() != null) {
                    Object object = RedisUtil.getCacheObject(RedisConstant.USER_KEY + StpUtil.getLoginIdAsLong());
                    return MapstructUtil.convert(object, clazz);
                }
            } catch (Exception ignored) {
                log.info("获取登录对象:'{}',失败!", clazz);
            }

        }
        //请先登录
        throw new NotLoginException(ErrorCodeEnum.USER_NOT_LOGIN.getValue(), DeviceEnum.MOBILE.getType(), "0");
    }

    /**
     * 登录
     *
     * @param id       标识ID
     * @param device   登录设备
     * @param timeout  过期时间
     * @param jwtKey   JWL key
     * @param jwtValue JWT value
     * @return
     */
    public static String loginToken(String id, String device, Long timeout, String jwtKey, String jwtValue) {
        //根据用户id，进行登录
        SaLoginModel saLoginModel = new SaLoginModel();
        if (StringUtil.isNotEmpty(device)) {
            saLoginModel.setDevice(device);
        }
        if (StringUtil.isNotEmpty(timeout) && timeout > 0) {
            saLoginModel.setTimeout(timeout);
        }
        if (StringUtil.isNotEmpty(jwtKey)) {
            saLoginModel.setExtra(jwtKey, jwtValue);
        }
        StpUtil.login(id, saLoginModel);

        // 获取当前会话的token值
        return StpUtil.getTokenValue();
    }

    /**
     * 退出当前登录
     */
    public static void logout() {
        try {
            // 当前会话注销登录
            StpUtil.logout();
        } catch (NotLoginException exception) {
            throw new LoginException("Not Log In : " + exception.getMessage());
        }
    }


}
